package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         1004,
		Discription: "最多把k个0变成1，求最长连续1的长度",
		Level:       2,
		Labels: map[string]int{
			"滑动窗口":   1,
		},
	})
}

func LongestOnes(nums []int, k int) int {
	count0 := 0
	left := 0
	right := 0
	res := 0
	for right < len(nums) && left <= right {
		if nums[right] == 0 {
			count0++
		}

		//滑动窗口中0的个数超过k时增加左区间
		for count0 > k && left <= right {
			if nums[left] == 0 {
				count0--
			}
			left++
		}

		res = max(res, right-left+1)
		right++
	}

	return res
}
